<template>
  <div style="width: 200px;">
          <a href="javascript:;" class="file">
              <input id="upload" type="file" @change="importfxx(this)"
               accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
                application/vnd.ms-excel" />
          </a>
</div>
</template>
<script>
  import axios from 'axios';
import Vue from 'Vue';
  export default {
        data() {
          return {
            message: '',
            fileTemp:''
          }
        },
        methods:{
        importfxx(obj) {
              let _this = this;
              let inputDOM = this.$refs.inputer;
              // 通过DOM取文件数据
              this.file = event.currentTarget.files[0];
              var rABS = false; //是否将文件读取为二进制字符串
              var f = this.file;
              var reader = new FileReader();
              //if (!FileReader.prototype.readAsBinaryString) {
              FileReader.prototype.readAsBinaryString = function(f) {
              var binary = "";
              var rABS = false; //是否将文件读取为二进制字符串
              var pt = this;
              var wb; //读取完成的数据
              var outdata;
              var reader = new FileReader();
              reader.onload = function(e) {
              var bytes = new Uint8Array(reader.result);
              var length = bytes.byteLength;
              for(var i = 0; i < length; i++) {
                      binary += String.fromCharCode(bytes[i]);
              }
              var XLSX = require('xlsx');
              if(rABS) {
                      wb = XLSX.read(btoa(fixdata(binary)), { //手动转化
                      type: 'base64'
                      });
              } else {
                      wb = XLSX.read(binary, {
                       type: 'binary'
                      });
              }
              outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);//outdata就是你想要的东西
              this.da = [...outdata]
              let arr = []
              console.log(outdata);
              let item={   //根据自己的角色
                rid:'',
                isstatus:'',
                rname:''
              };
              let gt=new Array;
              let o=0;
              for(var i=1;i<outdata.length;i++){
                  item=outdata[i];
                  /* item.isqy=outdata[i].isqy;
                  item.xlld=outdata[i].xlld; */
                  console.log(item);
                  gt.push(item);
              }

              console.log(gt);
               axios.post("http://localhost:8085/daoru",gt)
                    .then(v => {
                      console.log(v.data);
                      if(v.data.indexOf('角色编号重复！')>=0||v.data=='抱歉该文件没有数据！'){
                        _this.$message({
                              message: v.data,
                              type: 'error',
                              duration:3000
                        })

                      }else{
                        _this.$message({
                              message: v.data,
                              type: 'success'
                        })
                      }

                      }).catch(err => {
                      });
              this.da.map(v => {
                  let obj = {}
                  obj.id = v.id
                  obj.status = v.status
                  arr.push(obj)
              })
              let para = {
                   //withList: JSON.stringify(this.da)
                   withList: arr
              }
             /* _this.$message({
                    message: '请耐心等待导入成功',
                    type: 'success'
              }); */
        }
              reader.readAsArrayBuffer(f);
   }
          if(rABS) {
                reader.readAsArrayBuffer(f);
          } else {
                reader.readAsBinaryString(f);
          }
        },
       addLoading:function(){
        }
},
}

</script>

<style>
</style>
